****************
*MARF DID REGS *
****************

local controls=""
local outcomelist="  not_at_eviction_address " 

cap frame change default
cap frame drop results 
frame create results strL(label) b_evict se_evict b_evictXout se_evictXout n_persons n_person_years non_evict_base_mean y_base_n

***************
* Regressions *
***************
foreach pop in "all" "black"  "female"  {
	foreach popval of numlist 0 1 {
		if "`pop'"~="all" | `popval'~=0	{
			foreach per in "1_1" "2_2" {
				
				if "`per'"=="1_1" local did_est="6"
				if "`per'"=="2_2" local did_est="7"
				
				if "`per'"=="1_1" local base="4"
				if "`per'"=="2_2" local base="3"
								
				if "`pop'"~="all" {
					use `outcomelist' evicted ydum* evictXy* pik year black hispanic female noprior joint acs_placebo at_evic_n2_p0 using "${build}/linked_marf_event_study_panel.dta" if evicted~=. & acs_placebo==0  & at_evic_n2_p0== 1, clear
					gen black_miss = black == .
					gen female_miss = female == . 
					replace black = 0 if black_miss == 1
					replace female = 0 if female_miss == 1
					keep if  `pop'==`popval'
				}
				if "`pop'"=="all" use `outcomelist' evicted ydum* evictXy* pik year black hispanic female noprior joint acs_placebo at_evic_n2_p0 using "${build}/linked_marf_event_study_panel.dta" if evicted~=. & acs_placebo==0 & at_evic_n2_p0==1, clear
				
				gen ydum6_7 = ydum6==1 | ydum7==1 if ydum6!=. & ydum7!=.
				gen ydum7_8 = ydum7==1 | ydum8==1 if ydum7!=. & ydum8!=.
				gen ydum8_9 = ydum8==1 | ydum9==1 if ydum8!=. & ydum9!=.

				gen ydum3_4 = ydum3==1 | ydum4==1 if ydum3!=. & ydum4!=.
				gen ydum2_3 = ydum2==1 | ydum3==1 if ydum2!=. & ydum3!=.
				gen ydum1_2 = ydum1==1 | ydum2==1 if ydum1!=. & ydum2!=.

				gen evictXy6_7 = evictXy6==1 | evictXy7==1
				gen evictXy7_8 = evictXy7==1 | evictXy8==1
				gen evictXy8_9 = evictXy8==1 | evictXy9==1

				gen evictXy3_4 = evictXy3==1 | evictXy4==1
				gen evictXy2_3 = evictXy2==1 | evictXy3==1
				gen evictXy1_2 = evictXy1==1 | evictXy2==1

				foreach var of varlist `outcomelist' {
				di "On Pop = `pop'=`popval', Out = `var', Period = `per'"

					if "`per'"=="1_1" {
						areg  `var' evicted ydum1-ydum3 ydum5-ydum11  evictXy1-evictXy3 evictXy5-evictXy11 `controls' i.year, a(pik) cluster(pik) robust
					}
					if "`per'"=="2_2" {
						areg  `var' evicted ydum1 ydum2 ydum4-ydum11  evictXy1 evictXy2 evictXy4-evictXy11  `controls' i.year, a(pik) cluster(pik) robust
					}
					
					local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `per'" 
					
					* Save regression output
					local b_evicted= _b[evicted]
					local se_evicted=_se[evicted]
					local b_evictXout=_b[evictXy`did_est']
					local se_evictXout=_se[evictXy`did_est']
					
					unique pik if evicted==0 & ydum`base'==1 & e(sample)
					local y_base_n= r(sum)
					sum `var' if evicted==0 & ydum`base'==1 & e(sample)
					local non_evict_base_mean=r(mean)
					local n_person_years=e(N) 

					capture drop first
					capture drop sample
					gen sample=e(sample)
					bys pik sample: gen first=_n==1
					count if first==1 & sample==1
					local n_persons=.
							
					frame post results ("`label'")  (`b_evicted') (`se_evicted') (`b_evictXout') (`se_evictXout') (`n_persons') (`n_person_years') (`non_evict_base_mean') (`y_base_n')
					
					} // outcome loop 
				} // period loop 
			} // if statement on if groups are equal to 1 (doesn't run on 0)
	} // pop = 0/1 loop
} // pop loop

frame results: export delimited using "${Inputs}/Cook_marf_did.csv", replace 

import delimited "${Inputs}/Cook_marf_did.csv", clear 

split label, parse(",")
rename (label1 label2 label3 label4) (population outcome time_period specification)
rename (b_evict se_evict b_evictXout se_evictXout n_obs y_base_mean) (Coeff_evict  SE_evict Coeff_evictXout SE_evictXout N_Person_Years Mean_Non_Evict_Base)
drop *judgement*

export delimited using "${Inputs}/Cook_marf_did.csv", replace 